package com.changxianggu.cloud.edu.tool.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.changxianggu.cloud.edu.tool.constant.DbConstant;

import java.io.*;
import java.sql.*;

/**
 * 配置文件工具
 *
 * @author Mirren
 */
public class ConfigUtil {
    public static final String CONFIG_DIR = "config";
    public static final String CONFIG_FILE = "/ConfigDB.db";
    public static final String EXE_FILE_NAME = "/EduTool.exe";
    public static final String SHORT_CUT_BAT_FILE = "/MakeShortCut.bat";
    private static final String DRIVER = "org.sqlite.JDBC";
    private static final String DB_URL = "jdbc:sqlite:./config/ConfigDB.db";

    public static boolean insertOrUpdateEntity(String fileName, Object fieldValue) {
        boolean isOk = false;
        try {
            Entity entity = Entity.create(DbConstant.TABLE_NAME)
                    .set(DbConstant.TABLE_NAME_FIELD, fileName)
                    .set(DbConstant.TABLE_VALUE_FIELD, fieldValue);
            Db.use().insertOrUpdate(entity, DbConstant.TABLE_NAME_FIELD);
            isOk = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isOk;
    }

    /**
     * 查询
     *
     * @param fileName
     * @return
     */
    public static Entity selectEntity(String fileName) {
        Entity dbEntity = null;
        Entity entity = Entity.create(DbConstant.TABLE_NAME)
                .set(DbConstant.TABLE_NAME_FIELD, fileName);
        try {
            dbEntity = Db.use().get(entity);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dbEntity;
    }

    /**
     * 删除
     *
     * @param fileName
     */
    public static void deleteEntity(String fileName) {
        Entity entity = Entity.create(DbConstant.TABLE_NAME)
                .set(DbConstant.TABLE_NAME_FIELD, fileName);
        try {
            Db.use().del(entity);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static String getExeFilePath() {
        File configDir = new File(CONFIG_DIR);
        File parent = FileUtil.getParent(configDir, 2);
        String exeFilePath = parent.getAbsolutePath() + EXE_FILE_NAME;
        return exeFilePath;
    }

    /**
     * 查看是否存在配置文件数据库,如果不存在则创建
     *
     * @throws IOException
     */
    public static void copyResourcesFile(String fileName) throws IOException {
        File path = new File(CONFIG_DIR);

        if (!path.exists()) {
            path.mkdir();
        }
        File file = new File(CONFIG_DIR + fileName);
        if (!file.exists()) {
            InputStream fis = null;
            FileOutputStream fos = null;
            try {
                fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_DIR + fileName);
                fos = new FileOutputStream(CONFIG_DIR + fileName);
                byte[] buffer = new byte[1024];
                int byteread = 0;
                while ((byteread = fis.read(buffer)) != -1) {
                    fos.write(buffer, 0, byteread);
                }
            } finally {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
            }

        }
    }

}
